package cc.mrbird.tram.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.submission.domain.PullDown;
import cc.mrbird.submission.util.RestUtil;
import cc.mrbird.submission.util.WXPayUtil;
import cc.mrbird.tram.domain.Tram;
import cc.mrbird.tram.service.TramService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class TramController extends BaseController{
    @Value("${uploadPath}")
    private String uploadPath;

    @Autowired
    private TramService tramService;

    @RequestMapping("tram")
    public String tram(Model model) {

        return "tram/tram";
    }
    @Log("增加投诉信息")
    @RequestMapping("tram/addTram")
    @ResponseBody
    public ResponseBo addSubmission(Tram tram, @RequestPart(value="pic",required = false) MultipartFile pic) throws Exception {
        if(pic != null){
            String str = pic.getOriginalFilename();
            //截取图片名字之前字符串
            String str1=str.substring(0, str.indexOf("."));
            String frontPicName = str1+String.valueOf((int)((Math.random()*9+1)*1000000000));
            //截取图片名字之后字符串
            String str2=str.substring(0, str.indexOf("."));
            String afterPicName = str.substring(str2.length()+1, str.length());

            String picName = uploadPath + frontPicName+"."+afterPicName;
            File picFile = new File(picName);
            pic.transferTo(picFile);
            tram.setPicName(frontPicName+"."+afterPicName);
        }
        System.out.println("投诉类别:"+tram.getProcessing());
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        tram.setAcceptanceTime(dateString);
        tram.setFeedback(null);
        tram.setProcessing("26");
        if(tram.getFirst() == null){
            tram.setFirst("48");
        }
        tramService.saveTram(tram);
        String subid = tramService.findId();
        Long id = super.getCurrentUser().getUserId();
        if(id == 190){
            WXPayUtil wxPayUtil = new WXPayUtil();
            Map map = new HashMap();
            String idNotice = "111";//111
            String content = null;
            if(tram.getFirst().equals("48")){
                content = "你好，前台人员有提交新的充电桩投诉信息，投诉编号为："+subid+"，请注意查看";
            }else if(tram.getFirst().equals("47")){
                content = "你好，优先处理的充电桩投诉信息，投诉编号为："+subid+"，请注意查看！";
            }

            String keyid = "2";
            String random_number = wxPayUtil.generateNonceStr();
            String time_stamp = String.valueOf(System.currentTimeMillis());
            String key = "EL6UjSc4U1it6vaFrQ9GlGiMicEbRc0P";
            //通知编号
            map.put("id",idNotice);
            //通知内容
            map.put("content",content);
            //keyid
            map.put("keyid",keyid);
            //随机数
            map.put("random_number",random_number);
            //时间戳
            map.put("time_stamp",time_stamp);
            String sign = wxPayUtil.generateSignature(map,key);
            String url = "http://120.77.182.149:8999/OpenApi/tyxNotice";
            System.out.println(url);
            String data = "id="+idNotice+"&content="+content+"&keyid="+keyid+"&random_number="+random_number+"&time_stamp="+time_stamp+"&sign="+sign;

            RestUtil restUtil = new RestUtil();
            restUtil.interfaceUtil(url,data);
        }
        return ResponseBo.ok();
    }
    @Log("获取投诉信息列表")
    @RequestMapping("tram/tramList")
    @ResponseBody
    public Map<String, Object> submissionList(QueryRequest request, Tram tram) {

        return super.selectByPageNumSize(request, () -> this.tramService.getTramList(tram));
    }
    @Log("动态获取下拉选")
    @RequestMapping("tram/pullDown")
    @ResponseBody
    public List<PullDown> FindPullDown(PullDown pullDown ) {

        return tramService.FindPullDown(pullDown);

    }
    @Log("增加选配置")
    @RequestMapping("tram/addTramDown")
    @ResponseBody
    public ResponseBo addPullDown(PullDown pullDown )throws Exception {

        PullDown pullDown1 = tramService.savePullDown(pullDown);
        if(pullDown1 == null){
            tramService.addPullDown(pullDown);
            return ResponseBo.ok();
        }else{
            return ResponseBo.error("已存在！请重新填写");
        }
    }
    @Log("优先处理数量")
    @RequestMapping("tram/first")
    @ResponseBody
    public PullDown first(){
        PullDown pullDown = new PullDown();
        pullDown.setSubmissionValue(tramService.sumFirst());
        return pullDown;


    }

    @Log("根据id获取投诉信息")
    @RequestMapping("tram/getTramId")
    @ResponseBody
    public ResponseBo getByTramId(Tram tram) {
        String id = tram.getId();
        tram.setId(id);
        return ResponseBo.ok().put("data",tramService.getTramId(tram));
    }

    @Log("修改投诉信息")
    @RequestMapping("tram/updateTram")
    @ResponseBody
    public ResponseBo updateTram(Tram tram, @RequestPart(value="pic",required = false) MultipartFile pic) throws Exception {
        if(pic != null){
            String str = pic.getOriginalFilename();
            //截取图片名字之前字符串
            String str1=str.substring(0, str.indexOf("."));
            String frontPicName = str1+String.valueOf((int)((Math.random()*9+1)*1000000000));
            //截取图片名字之后字符串
            String str2=str.substring(0, str.indexOf("."));
            String afterPicName = str.substring(str2.length()+1, str.length());

            String picName = uploadPath + frontPicName+"."+afterPicName;
            File picFile = new File(picName);
            pic.transferTo(picFile);
            tram.setPicName(frontPicName+"."+afterPicName);
        }
        Long id = super.getCurrentUser().getUserId();
        if(id == 190){
            tram.setProcessing("未处理");
        }
        if(tram.getProcessing().equals("已处理")||tram.getProcessing()=="已处理"){
            Date currentTime = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(currentTime);
            tram.setProcessingTime(dateString);
        }
        String processingTime = tram.getProcessingTime();
        if(processingTime == ""){
            System.out.println("processingTime: "+processingTime);
            tram.setProcessingTime(null);
        }
        tramService.updateTram(tram);
        return ResponseBo.ok();
    }

    @Log("投诉信息撤销")
    @RequestMapping("tram/cancelTram")
    @ResponseBody
    public ResponseBo cancelSubmission(Tram tram )throws Exception{

        tramService.cancelTram(tram);
        return ResponseBo.ok();
    }
}
